import { _decorator, Component, Node } from 'cc';
import { Pool } from '../../sgame/pool/Pool';
import { random } from 'cc';
import { randomRangeInt } from 'cc';
import { lwarn } from '../../sgame/logger/Logger';
const { ccclass, property } = _decorator;

class Person {
    constructor(public name: string, public age: number, public sex: number) { }
}

class MyObject {
    constructor(public value: number) { }
    // 其他方法和属性...
}

@ccclass('PoolTest')
export class PoolTest extends Component {
    private pool: Pool<Person> = new Pool(Person, (person: Person) => {
        console.log("on get Person", person);
    }, (person: Person) => {
        console.log("on put Person", person);
    }, (person: Person) => {
        console.log("on new Person", person);
    }, (person: Person) => {
        console.log("on release Person", person);
    });

    private _person: Person[] = [];

    private _randomNameChar: string = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    start() {
        lwarn('this.newPerson(5)');
        this.newPerson(5);
        lwarn('this.putPerson()');
        this.putPerson();
        lwarn('this.newPerson(10)');
        this.newPerson(10);
        lwarn('this.clearPerson()');
        this.clearPerson();
    }

    private newPerson(len) {
        for (let i = 0; i < len; i++) {
            const name = this.randomName(randomRangeInt(0, 10));
            const age = randomRangeInt(0, 100);
            const sex = randomRangeInt(0, 2);
            this._person.push(this.pool.get(name, age, sex));
        }
    }

    private putPerson() {
        for (let i = 0; i < this._person.length; i++) {
            this.pool.put(this._person[i]);
        }
    }

    public clearPerson() {
        this.pool.clear();
    }

    private randomName(len: number) {
        let name: string = '';
        for (let i = 0; i < len; i++) {
            name += this._randomNameChar[randomRangeInt(0, this._randomNameChar.length)];
        }

        return name;
    }
}

